/**
 * Copyright (c) 2012 Nokia Corporation.
 * All rights reserved.
 *
 * For the applicable distribution terms see the license text file included in
 * the distribution.
 */

#ifndef PLACESLISTMODEL_H
#define PLACESLISTMODEL_H

#include <QAbstractListModel>
#include <QLandmarkManager>
#include <QTimer>

QTM_USE_NAMESPACE

class PlacesListModel : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(QString categoryName READ categoryName NOTIFY categoryNameChanged)

public: // Roles

    enum PlacesListModelRoles {
        IdRole = 0,
        NameRole,
        AddressRole,
        LatitudeRole,
        LongitudeRole
    };

public:
    explicit PlacesListModel(QObject *parent = 0);
    explicit PlacesListModel(const PlacesListModel &model, QObject *parent = 0);
    virtual ~PlacesListModel();

public: // From QAbstractListModel
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    int rowCount(const QModelIndex &parent = QModelIndex()) const;

public:
    QString categoryName() const;

public slots:
    void updatePlacesData(const QVariant &data);
    void updateVisibleLandmarks();
    void setFilter(const QString &filterText);

private slots:
    void clearVisibleLandmarks();
    void addVisibleLandmark(const QLandmark &landmark);
    void saveNewLandmarks();

private:
    void initialize();
    void removeOldLandmarks();

signals:
    void visibleItemsUpdated();
    void categoryNameChanged(QString name);
    void error(const QString &errorString);

private: // Data
    QLandmarkManager *m_landmarkManager; // Owned
    QList<QLandmark> m_allLandmarks;
    QList<QLandmark> m_visibleLandmarks; // There are the ones you get with data()
    QLandmarkCategory m_category;
    QTimer m_updateTimer;
    QString m_filter;
    bool m_initialized;
};

Q_DECLARE_METATYPE(PlacesListModel)

#endif // PLACESLISTMODEL_H
